package com.tools.traceTool
{
	import flash.events.AsyncErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.events.TimerEvent;
	import flash.net.LocalConnection;
	import flash.utils.Timer;
// DEF #####################################################
internal class ReceiverBase
{
// DEF #####################################################
private 	var	reconnecring:Boolean=false;
protected	var	callerContent:ReceiverCore;
private 	var localConnection:LocalConnection

public function ReceiverBase(){}
public function	set client(callerThis:ReceiverCore):void{callerContent=callerThis;connect();}
//	it start Listening (connection)
public	function	connect():void
{
	close();
	localConnection=new LocalConnection();
	localConnection.client=callerContent;
	localConnection.allowDomain('*','localhost');
	localConnection.allowInsecureDomain('*','localhost');
	localConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
	localConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
	localConnection.addEventListener(StatusEvent.STATUS, onStatus);
	reconnecring=true;
	connecting();
}
private	function	connecting(e:TimerEvent=null):void
{
	if(!reconnecring && e is TimerEvent)
	{
		Timer(e.target).removeEventListener(TimerEvent.TIMER,connecting);
		Timer(e.target).stop();
		reconnecring=false;
		return;
	}
	callerContent['callback']('localMsg',"    Trying to connect... ");
	try {localConnection.connect(TraceConfig.CONNECTION_ID); reconnecring=false; return;}
	catch(ae:ArgumentError)
	{
		callerContent['callback']('localMsg',"    Can't connect...the connection name is already being used by another SWF... ");
	}
	catch(te:TypeError)
	{
		callerContent['callback']('localMsg','TypeError... it should`nt be ... connection error... ');
		callerContent['callback']('localMsg','this line is missing somewhere:"localConnection=new LocalConnection();"'+te);
		callerContent['callback']('localMsg',''+te);
	}
	catch(ee:Error)
	{
		callerContent['callback']('localMsg','	Unknown connection error...\n'+ee);
	}
	if(e==null)
	{
		var	timer:Timer=new	Timer(TraceConfig.SIGN);
			timer.addEventListener(TimerEvent.TIMER,connecting);
			timer.start();
	}
}
private	function	close():void
{
	try
	{
		localConnection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		localConnection.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
		localConnection.removeEventListener(StatusEvent.STATUS, onStatus);
		localConnection.close();
		localConnection=undefined;
	}catch(e:Error){}
}
// Events #####################################################
private	function	onAsyncError(e:AsyncErrorEvent):void{		callerContent?callerContent['callback']('Event',e):null;}
private	function	onSecurityError(e:SecurityErrorEvent):void{	callerContent?callerContent['callback']('Event',e):null;}
private	function	onStatus(e:StatusEvent):void
{
	if(e.level=='status'||e.level=='error'){return;}			callerContent?callerContent['callback']('Event',e):null;
}
// END #####################################################
}	//class
}	//package
// END #####################################################